package com.tony.jrp.util;

import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.httpproxy.impl.ParseUtils;

import java.time.Instant;
import java.util.List;

public class HttpUtils {
    public static Boolean isChunked(MultiMap headers) {
        List<String> te = headers.getAll("transfer-encoding");
        if (te != null) {
            boolean chunked = false;
            for (String val : te) {
                if (val.equals("chunked")) {
                    chunked = true;
                } else {
                    return null;
                }
            }
            return chunked;
        } else {
            return false;
        }
    }

    public static Instant dateHeader(MultiMap headers) {
        String dateHeader = headers.get(HttpHeaders.DATE);
        if (dateHeader == null) {
            List<String> warningHeaders = headers.getAll("warning");
            if (!warningHeaders.isEmpty()) {
                for (String warningHeader : warningHeaders) {
                    Instant date = ParseUtils.parseWarningHeaderDate(warningHeader);
                    if (date != null) {
                        return date;
                    }
                }
            }
            return null;
        } else {
            return ParseUtils.parseHeaderDate(dateHeader);
        }
    }
}
